package com.example.uit_management;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ArrayAdapterSchedule_Semester extends BaseAdapter {
	// XML node keys
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;   
    
    public ArrayAdapterSchedule_Semester(Activity a, ArrayList<HashMap<String, String>> d) {    	
        activity = a;        
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }   
    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.schedule_row_vs2, null);
        TextView ca = (TextView)vi.findViewById(R.id.time_TextView_shedule_vs2); 
        TextView thu = (TextView)vi.findViewById(R.id.timeday_TextView_schedule_vs2);
        TextView Ma = (TextView)vi.findViewById(R.id.courseID_shedule_vs2);
        TextView Ten = (TextView)vi.findViewById(R.id.subjectID_schedule_vs2);
        TextView TenGiaoVien = (TextView)vi.findViewById(R.id.lecturerName_schedule_vs2);
        TextView Loai = (TextView)vi.findViewById(R.id.regCourseType_schedule_vs2);
        TextView Phong = (TextView)vi.findViewById(R.id.RoomID_schedule_vs2); 
        TextView TC = (TextView)vi.findViewById(R.id.subjectCredit_schedule_vs2); 
		TextView NienKhoa_schedule = (TextView)vi.findViewById(R.id.NienKhoa_schedule_vs2);
		
        
		//-- set hoc ki
		NienKhoa_schedule.setVisibility(View.GONE);		
		NienKhoa_schedule.setBackgroundDrawable(null);
		NienKhoa_schedule.setHeight(0);
		
		////////////////////////
		// hien hoc ki hoc
		//////////////////////
		// Ap dung cho 1 hoac tat ca hoc ki
		if(Activity_list_schedule_vs2.getSemester_Spinner().toString().equals("Tất cả")|| true){ 
		if(position !=0){
			if(!(data.get(position-1).get(JsonParser_course.KEY_semesterYear).equals(data.get(position).get(JsonParser_course.KEY_semesterYear)))){
		/*		Subname_score.setBackgroundResource(R.drawable.border_textview_score_red);
			    classID_core.setBackgroundResource(R.drawable.border_textview_score_red);
			    scorefinal.setBackgroundResource(R.drawable.border_textview_score_red);*/
			// thay doi nien khoa
				Log.e(ArrayAdapterSchedule_Semester.class.toString(), "dung 3.1");
				// set visible
				NienKhoa_schedule.setVisibility(View.VISIBLE);        			
				// set text
				NienKhoa_schedule.setText("Học Kỳ :"+data.get(position).get(JsonParser_course.KEY_semesterIndex)+"_Nam :"+data.get(position).get(JsonParser_course.KEY_semesterYear));
				//set height        
				//Semester.setHeight(Activity_Score.Height_sore);
		       //Subname_score.setPaintFlags(Subname_score.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
				// set backgroud
				NienKhoa_schedule.setBackgroundResource(R.drawable.blue_bar6);
		
			}else if(!(data.get(position-1).get(JsonParser_course.KEY_semesterIndex).equals(data.get(position).get(JsonParser_course.KEY_semesterIndex)))){
				/*Subname_score.setBackgroundResource(R.drawable.border_textview_score_red);
			    classID_core.setBackgroundResource(R.drawable.border_textview_score_red);
			    scorefinal.setBackgroundResource(R.drawable.border_textview_score_red);*/
				// thay doi nien khoa
				Log.e(ArrayAdapterSchedule_Semester.class.toString(), "dung 3.2");
				// set visible
				NienKhoa_schedule.setVisibility(View.VISIBLE);        			
				// set text
				NienKhoa_schedule.setText("Học Kỳ :"+data.get(position).get(JsonParser_course.KEY_semesterIndex)+"_Nam :"+data.get(position).get(JsonParser_course.KEY_semesterYear));
				//set height
				NienKhoa_schedule.setHeight(Activity_Score.Height_sore);
				// set backgroud
				NienKhoa_schedule.setBackgroundResource(R.drawable.blue_bar6);
				}        		
			}		
			else if (position==0){
				// thay doi nien khoa
				Log.e(ArrayAdapterSchedule_Semester.class.toString(), "dung 3.3: "+Activity_Score.Height_sore);
				// set visible
				NienKhoa_schedule.setVisibility(View.VISIBLE);        			
				// set text
				NienKhoa_schedule.setText("Học Kỳ :"+data.get(position).get(JsonParser_course.KEY_semesterIndex)+"_Năm :"+data.get(position).get(JsonParser_course.KEY_semesterYear));
				//set height        
				NienKhoa_schedule.setHeight(Activity_Score.Height_sore);
				// set backgroud
				NienKhoa_schedule.setBackgroundResource(R.drawable.blue_bar6);
			}			
		
		} 
        
        HashMap<String, String> array = new HashMap<String, String>();
        array = data.get(position);
        String a= "Bao cao :"+ position;
        //Log.e(ArrayAdapterSchedule_Semester.class.toString(), a);
        // Setting all values in listview
        Log.e(ArrayAdapterSchedule_Semester.class.toString(), "dung 4 ");
        Log.e(ArrayAdapterSchedule_Semester.class.toString(), "dung 5 ");
        Ma.setText(array.get(JsonParser_course.KEY_courseID));
        Log.e(ArrayAdapterSchedule_Semester.class.toString(), "dung 6 ");
        Ten.setText(array.get(JsonParser_course.KEY_subjectName));
        Log.e(ArrayAdapterSchedule_Semester.class.toString(), "dung 7 ");  
        try
        {
        	Log.e(ArrayAdapterSchedule_Semester.class.toString(), "aaa11 " +(array.get(JsonParser_course.KEY_subjectPractice)));
        	Log.e(ArrayAdapterSchedule_Semester.class.toString(), "aaa21 " +(array.get(JsonParser_course.KEY_subjectTheory)));
           
        	Log.e(ArrayAdapterSchedule_Semester.class.toString(), "aaa1 " +Integer.parseInt(array.get(JsonParser_course.KEY_subjectPractice)));
        	Log.e(ArrayAdapterSchedule_Semester.class.toString(), "aaa2 " +Integer.parseInt(array.get(JsonParser_course.KEY_subjectTheory)));
           int SoTC = Integer.parseInt(array.get(JsonParser_course.KEY_subjectPractice)) + Integer.parseInt(array.get(JsonParser_course.KEY_subjectTheory));
        	TC.setText(""+SoTC);

        }catch(Exception e)
        {        
        }
        Log.e(ArrayAdapterSchedule_Semester.class.toString(), "dung 9 :"+array.get(JsonParser_course.KEY_courseTime) );
        ca.setText(array.get(JsonParser_course.KEY_courseTime).toString());
        Log.e(ArrayAdapterSchedule_Semester.class.toString(), "dung 8 ");
        thu.setText(array.get(JsonParser_course.KEY_courseDay));
        TenGiaoVien.setText(array.get(JsonParser_course.KEY_lecturerName)); 
        Log.e(ArrayAdapterSchedule_Semester.class.toString(), "dung 9 ");       
        Loai.setText(array.get(JsonParser_course.KEY_courseType));
        Phong.setText(array.get(JsonParser_course.KEY_roomID));        
        Log.e(ArrayAdapterSchedule_Semester.class.toString(), "VAo get view 3");
        return vi;
    }
}